package org.luxor.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.luxor.commons.core.utils.BeanUtils;
import org.luxor.system.entity.SysRoleEntity;
import org.luxor.system.entity.SysUserRoleRefEntity;
import org.luxor.system.mapper.SysRoleMapper;
import org.luxor.system.service.ISysRoleService;
import org.luxor.system.service.ISysUserRoleRefService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色
 *
 * @author Mark @date 2020-07-26 16:52:22
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements ISysRoleService {

    @Resource
    private ISysUserRoleRefService sysUserRoleRefService;

    @Override
    public IPage<SysRoleEntity> page(IPage<SysRoleEntity> page, Map query) {
        SysRoleEntity queryEntity = BeanUtils.toObject(SysRoleEntity.class, query);
        return super.page(page, Wrappers.query(queryEntity));
    }

    @Override
    public Collection<SysRoleEntity> listByUserId(Long userId) {
        Collection<SysUserRoleRefEntity> userRoleRefs = sysUserRoleRefService.listByUserId(userId);
        Set<Long> roleIds = userRoleRefs.stream().map(SysUserRoleRefEntity::getRoleId).collect(Collectors.toSet());
        Collection<SysRoleEntity> roles = getBaseMapper().selectBatchIds(roleIds);
        return roles;
    }
}
